package com.s666.demo.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

	public static final Date MIN_DATE = new Date(0);

	public static final Date MAX_DATE = new Date(3155378975999999999L);

	private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private static final String DATE_FORMAT = "yyyy-MM-dd";
	private static final String DATE_SHORT = "yyyyMMdd";



	/**
	 *
	 * @param d Date格式
	 * @param pattern "yyyy-MM-dd".
	 *
	 * @return String
	 */
	public static String format(Date d, String pattern) {
		return new SimpleDateFormat(pattern).format(d);
	}

	/**
	 *
	 * @param strDate 2019-07-30
	 * @param pattern yyyy-MM-dd HH:mm:ss
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String strDate, String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 以<code>format</code>解析<code>dateString</code>产生日期
	 * 
	 * @param dateString
	 * @param format
	 * @return
	 */
	public static Date getDate(String dateString, String format) {
			try {
				SimpleDateFormat df = new SimpleDateFormat(format);
				return df.parse(dateString);
			} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 将时间转换为 "yyyy-MM-dd 00:00:00
	 * @param date
	 * @return Date
	 * @throws ParseException
	 */
	public static Date format(Date date) {

		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
		String dateTime = dateFormat.format(date);
		try{
			return dateFormat.parse(dateTime);
		}catch (ParseException e) {
			throw new RuntimeException();
		}

	}

	public static Date getDateFromDay(String date, int interval) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parseToDate(date));
		cal.add(Calendar.DATE, interval);
		return cal.getTime();
	}

	public static Date getDateFromDay(Date date, int interval) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, interval);
		return cal.getTime();
	}

	/**
	 * 计算当前日期的前一月
	 * @return
	 */
	public static String getCurrentPreMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		//过去一月
		c.setTime(new Date());
		c.add(Calendar.MONTH, -1);
		Date m = c.getTime();
		String mon = format.format(m);
		return mon;

	}

	/**
	 * 计算两个日期之间的小时
	 */
	public static int getHoursBetweenTwoDates(long beginDate, long endDate) {
		long result = 0;
		if (beginDate > endDate) {
			result = (beginDate - endDate) / 3600000;
		} else {
			result = (endDate - beginDate) / 3600000;
		}
		return (int) result;
	}

	public static long getCurrentMilliTime() {
		return System.currentTimeMillis();
	}

	/**
	 * 以<code>format</code>格式把<code>date</code>转换成字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDateString(Date date, String format) {
		SimpleDateFormat timeFormatter = new SimpleDateFormat(format);
		String dateString = timeFormatter.format(date);
		return dateString;
	}

	/**
	 * 秒数转换成当前时间字符串，形如yyyy-MM-dd HH:mm:ss
	 * 
	 * @param timeLong
	 *            离January 1, 1970, 00:00:00 GMT的秒数
	 * @return 当前时间字符串
	 */
	public static String longToTime(long timeLong) {
		Date date = new Date(timeLong * 1000);
		return getDateString(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 计算两日期之间的间隔
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static int getTimeDistance(Date beginDate , Date endDate ) {
		Calendar beginCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		long beginTime = beginCalendar.getTime().getTime();
		long endTime = endCalendar.getTime().getTime();
		int betweenDays = (int)((endTime - beginTime) / (1000 * 60 * 60 *24));
		endCalendar.add(Calendar.DAY_OF_MONTH, -betweenDays);
		endCalendar.add(Calendar.DAY_OF_MONTH, -1);
		if(beginCalendar.get(Calendar.DAY_OF_MONTH)==endCalendar.get(Calendar.DAY_OF_MONTH)) {
			return betweenDays + 1;
		} else {
			return betweenDays + 0;
		}
	}

	/**
	 * 把一个形如"yyyy-mm-dd hh24:mi:ss"时间型转换成离January 1, 1970, 00:00:00 GMT的秒数
	 * 
	 * @param dateTime
	 * @return <code>dateTime</code>离January 1, 1970, 00:00:00 GMT的秒数
	 */
	public static long dateTimeToLong(String dateTime) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			Date date = dateFormat.parse(dateTime);
			return date.getTime() / 1000;
		} catch (ParseException e) {
			// SysLogger.error(SysUtil.class, e);
			return 0;
		}
	}

	/**
	 * 计算出HH:mm:ss的距离00:00:00的秒数
	 * 
	 * @param time
	 *            格式如：HH:mm:ss
	 * @return
	 */
	public static long timeToLong(String time) {
		String date = getCurrentDate();
		String dateTime = date + " " + time;
		return dateTimeToLong(dateTime) - dateToLong(date);
	}

	/**
	 * 把一个形如"yyyy-MM-dd"日期型转换成一个长整数
	 */
	public static long dateToLong(String date) {
		return getDate(date, "yyyy-MM-dd").getTime() / 1000;
	}

	public static long dateToLongMilles(String date) {
		return getDate(date, "yyyy-MM-dd").getTime();
	}

	/**
	 * 年<code>year</code>月<code>month</code>具有的天数
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getDayNumOfMonth(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, 0);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取与时间<code>dateTime</code>距离<code>second</code>秒的时间 dateTime形如yyyy-MM-dd
	 * HH:mm:ss
	 * 
	 * @param dateTime
	 * @param second
	 * @return
	 */
	public static String calculateDateTime(String dateTime, int second) {
		return longToTime(dateTimeToLong(dateTime) + second);
	}

	/**
	 * 判断今天是否这个月的最后一天
	 * 
	 * @return
	 */
	public static boolean isTodayLastOfMonth() {
		return getCurrentDayOfMonth() == getDayNumOfMonth(getCurrentYear(),
				getCurrentMonth());
	}

	/**
	 * 当前日期是几号
	 * 
	 * @return
	 */
	public static int getCurrentDayOfMonth() {
		return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
	}

	public static String getCurrentDayOfMonthStr() {
		return String
				.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
	}

	/**
	 * 获取<code>startTime</code>和<code>endTime</code>之前的月列表
	 * <code>startTime</code>和<code>endTime</code>的格式以yyyy-MM开头
	 * 
	 * @param startTime
	 * @param endTime
	 * @param format
	 *            返回的列表每项的格式
	 * @return
	 * @throws Exception
	 */
	public static List<String> getAllMonthBetween(String startTime,
			String endTime, String format) {
		List<String> allMonth = new ArrayList<String>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		Date startMonth = null;
		Date endMonth = null;
		try {
			startMonth = df.parse(startTime);
			endMonth = df.parse(endTime);
			Calendar cal = Calendar.getInstance();
			cal.setTime(startMonth);
			while (!cal.getTime().after(endMonth)) {
				allMonth.add(getDateString(cal.getTime(), "yyyyMM"));
				cal.add(Calendar.MONDAY, 1);
			}
			return allMonth;
		} catch (Exception e) {
			// SysLogger.error(SysUtil.class, e);
			return null;
		}

	}

	/**
	 * 根据<code>day</code>获取星期中文名，day==1时星期一
	 * 
	 * @param day
	 * @return
	 */
	public static String getWeekDay(int day) {
		if (day == 1)
			return "星期一";
		else if (day == 2)
			return "星期二";
		else if (day == 3)
			return "星期三";
		else if (day == 4)
			return "星期四";
		else if (day == 5)
			return "星期五";
		else if (day == 6)
			return "星期六";
		else
			return "星期天";
	}

	/**
	 * 得到当前日期字符串，形如：yyyy-MM-dd
	 */
	/*
	 * public static String getCurrentDate() { return getDateString(new Date(),
	 * "yyyy-MM-dd"); }
	 */

	/**
	 * 获取凌晨时间
	 * 
	 * @return
	 */
	public static String getWeeHours() {
		return "00:00:00";
	}

	/**
	 * 得到当前时间yyyy-MM-dd HH:mm:ss
	 */
	/*
	 * public static String getCurrentDateTime() { return getDateString(new
	 * Date(), "yyyy-MM-dd HH:mm:ss"); }
	 */

	/**
	 * 得到当前时间yyyyMMdd
	 */
	public static String getCurrentDateAsId() {
		return getDateString(new Date(), "yyyyMMdd");
	}

	/**
	 * 得到当前时间yyyyMMddHHmmss
	 */
	public static String getCurrentDateTimeAsId() {
		return getDateString(new Date(), "yyyyMMddHHmmss");
	}

	/**
	 * 得到当前时间yyyyMMddHHmmssSSS
	 */
	public static String getCurrentDateTimeDetailAsId() {
		return getDateString(new Date(), "yyyyMMddHHmmssSSS");
	}

	/**
	 * 得到当前时间HH:mm:ss
	 * 
	 * @return
	 */
	public static String getCurrentTime() {
		return getDateString(new Date(), "HH:mm:ss");
	}

	/**
	 * 得到当前时间yyyy-MM-dd HH:mm:ssSSS
	 * 
	 * @return
	 */
	public static String getCurrentTime2() {
		return getDateString(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 计算出当前时间HH:mm:ss距离00:00:00的秒数
	 * 
	 * @return
	 */
	public static long getCurrentLongTime() {
		return timeToLong(getCurrentTime());
	}

	/**
	 * 
	 * @return 当前时间离January 1, 1970, 00:00:00 GMT的毫秒数
	 */
	public static long getCurrentLongDateTimeLong() {
		return (long) (new Date()).getTime();
	}

	/**
	 * 
	 * @return 当前时间离January 1, 1970, 00:00:00 GMT的秒数
	 */
	public static long getCurrentLongDateTime() {
		return (long) (new Date()).getTime() / 1000;
	}

	/**
	 * 得到当前年份
	 */
	public static int getCurrentYear() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	public static String getCurrentYearStr() {
		return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
	}

	/**
	 * 得到当前月份,不包括年
	 */
	public static int getCurrentMonth() {
		return Calendar.getInstance().get(Calendar.MONTH) + 1;
	}

	public static String getCurrentMonthStr() {
		return String.valueOf(Calendar.getInstance().get(Calendar.MONTH) + 1);
	}

	/**
	 * 得到当前小时
	 */
	public static int getCurrentHour() {
		Calendar cal = Calendar.getInstance();
		int curHour = cal.get(Calendar.HOUR_OF_DAY);
		return curHour;
	}

	/**
	 * 得到当前的分钟
	 * 
	 * @return
	 */
	public static int getCurrentMinute() {
		Calendar cal = Calendar.getInstance();
		int curMinute = cal.get(Calendar.MINUTE);
		return curMinute;
	}

	/**
	 * 获取当前时间的一个月后的时间
	 * 
	 * @return
	 */
	public static Date getNextMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 1);
		return calendar.getTime();
	}

	/**
	 * 计算两个日期之间的天数
	 */
	public static int getDaysBetweenTwoDates(long beginDate, long endDate) {
		long result = 0;
		if (beginDate > endDate)
			result = (beginDate - endDate) / 86400;
		else
			result = (endDate - beginDate) / 86400;
		return (int) result;
	}

	/**
	 * 得到某天的日期 interval 距给定日期的天数
	 */
	public static Date getDateFromToday(int interval) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, interval);
		Date date = cal.getTime();
		return date;
	}

	public static Date getDateFromOneDay(String date, int interval) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parseToDate(date));
		cal.add(Calendar.DATE, interval);
		return cal.getTime();
	}

	/**
	 * 将秒数转换成时间字符串：如HH时MM分SS秒
	 * 
	 * @param second
	 * @return
	 */
	public static String secondToTimeString(long second) {
		StringBuffer timeStr = new StringBuffer(10);
		long hh24 = second / 3600;
		long surplus = second % 3600;

		long mi = surplus / 60;
		long ss = surplus % 60;

		if (hh24 > 0) {
			timeStr.append(hh24);
			timeStr.append("时");
		}

		if (mi > 0) {
			timeStr.append(mi);
			timeStr.append("分");
		}

		if (timeStr.length() == 0 || ss > 0) {
			timeStr.append(ss);
			timeStr.append("秒");
		}

		return timeStr.toString();
	}

	/**
	 * 计算两个时间点间的时间差
	 */
	public static String diffTwoTime(String time1, String time2) {
		if (time1 == null || time2 == null)
			return "";
		long diffTime = dateTimeToLong(time2) - dateTimeToLong(time1);
		return secondToTimeString(diffTime);
	}

	/**
	 * 计算两个时间点间的时间差
	 */
	public static long diffTwoTimeToLong(String time1, String time2) {
		if (time1 == null || time2 == null)
			return 0;
		return dateTimeToLong(time2) - dateTimeToLong(time1);
	}

	/**
	 * Parse date like yyyy-MM-dd
	 */
	public static Date parseToDate(String d) {
		try {
			return new SimpleDateFormat(DATE_FORMAT).parse(d);
		} catch (Exception e) {
			//
		}
		return null;
	}

	/**
	 * Parse date and time like yyyy-MM-dd hh:mm:ss
	 */
	public static Date parseToDateTime(String dt) {
		try {
			return new SimpleDateFormat(DATETIME_FORMAT).parse(dt);
		} catch (Exception e) {
			//
		}
		return null;
	}

	/**
	 * Parse date and time like yyyyMMdd
	 */
	public static Date parseShortDate(String dt) {
		try {
			return new SimpleDateFormat(DATE_SHORT).parse(dt);
		} catch (Exception e) {
			//
		}
		return null;
	}

	/**
	 * Convert date to String like "yyyyMMdd"
	 */
	public static String formatShortDate(Date d) {
		return new SimpleDateFormat(DATE_SHORT).format(d);
	}

	/**
	 * Convert date to String like "yyyy-MM-dd".
	 */
	public static String formatDate(Date d) {
		return new SimpleDateFormat(DATE_FORMAT).format(d);
	}

	/**
	 * Convert date to String like "yyyy-MM".
	 */
	public static String formatDates(Date d) {
		return new SimpleDateFormat("yyyy-MM").format(d);
	}

	/**
	 * Convert date and time to string like "yyyy-MM-dd HH:mm".
	 */
	public static String formatDateTime(Date d) {
		return new SimpleDateFormat(DATETIME_FORMAT).format(d);
	}

	/**
	 * get Current Date to string like "yyyyMMdd".
	 */
	public static String getCurrentShortDate() {
		return new SimpleDateFormat(DATE_SHORT).format(new Date());
	}

	/**
	 * get Current Date to string like "yyyy-MM-dd".
	 */
	public static String getCurrentDate() {
		return new SimpleDateFormat(DATE_FORMAT).format(new Date());
	}

	/**
	 * get Current Date to string like "yyyy-MM-dd HH:mm"
	 */
	public static String getCurrentDateTime() {
		return new SimpleDateFormat(DATETIME_FORMAT).format(new Date());
	}

	/**
	 * get Timestamp from string like "yyyyMMdd"
	 */
	public static Timestamp getShortTimestamp(String str) {
		if (str == null)
			return null;
		Date date = parseShortDate(str);
		long currrentTime = date.getTime();
		return new Timestamp(currrentTime);
	}

	/**
	 * get Timestamp from string like "yyyy-MM-dd"
	 */
	public static Timestamp getDefaultTimestamp(String str) {
		if (str == null)
			return null;
		Date date = parseToDate(str);
		long currrentTime = date.getTime();
		return new Timestamp(currrentTime);
	}

	/**
	 * get Timestamp from string like "yyyy-MM-dd HH:mm:ss".
	 */
	public static Timestamp getTimestamp(String str) {
		Timestamp tt = null;
		int len = str.length();
		Date date = null;
		switch (len) {
		case 8: {
			date = parseShortDate(str);
			break;
		}
		case 10: {
			date = parseToDate(str);
			break;
		}
		case 19: {
			tt = Timestamp.valueOf(str);
			break;
		}
		case 21: {
			tt = Timestamp.valueOf(str);
			break;
		}
		default:
			break;
		}

		if (tt == null) {
			long currrentTime = date.getTime();
			tt = new Timestamp(currrentTime);
		}
		return tt;
	}

	/**
	 * 
	 * ��ȡϵͳ��ǰʱ��
	 * 
	 * @return �������� java.sql.Date
	 */
	public static java.sql.Date getCurrentSqlDate() {
		long currrentTime = new Date().getTime();
		return new java.sql.Date(currrentTime);
	}

	/**
	 * ��dateת��Ϊjava.sql.Timestamp
	 * 
	 * @param date
	 * @return
	 */
	public static Timestamp getTimestamp(Date date) {
		if (date == null)
			return null;
		long currrentTime = date.getTime();
		return new Timestamp(currrentTime);
	}

	/**
	 * ��dateת��Ϊjava.sql.Timestamp
	 * @return
	 */
	public static Timestamp getTimestamp() {

		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * ��dateת��Ϊjava.sql.Date
	 * 
	 * @param date
	 * @return
	 */
	public static java.sql.Date getSqlDate(Date date) {
		long currrentTime = date.getTime();
		return new java.sql.Date(currrentTime);
	}

	/**
	 * orcale toDateƴ�Ӻ���;
	 * 
	 * @return
	 */
	public static String toOrcaleDate(String date) {
		return "TO_DATE('" + date + "','yyyy-mm-dd hh24:mi:ss')";
	}

	public static String toOrcaleDate(Date d) {
		return "TO_DATE('" + DateUtil.formatDateTime(d)
				+ "','yyyy-mm-dd hh24:mi:ss')";
	}

	public static String toOrcaleShortDate(Date d) {
		return "TO_DATE('" + DateUtil.formatShortDate(d) + "','yyyy-mm-dd')";
	}

	public static String getCurrentFormatime() {
		GregorianCalendar c = new GregorianCalendar();
		SimpleDateFormat df = new SimpleDateFormat("yyyy��MM��dd�� HHʱmm��ss��");
		return (df.format(c.getTime()));

	}

	// System.out.println(DateUtil.dateCompare( DateUtil.getDate("20150101",
	// "yyyyMMdd"),new Date()));
	public static boolean dateCompare(Date dat1, Date dat2) {
		boolean dateComPareFlag = true;
		if (dat2.compareTo(dat1) != 1) {
			dateComPareFlag = false; //
		}
		return dateComPareFlag;
	}

	/**
	 * 
	 * @param in
	 * @param years
	 * @return
	 * @pre in != null
	 */
	public static Date addYears(Date in, int years) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(in);
		cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + years);
		return cal.getTime();
	}

	/**
	 * 
	 * @param in
	 * @param months
	 * @return
	 * @pre in != null
	 */
	public static Date addMonths(Date in, int months) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(in);
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + months);
		return cal.getTime();
	}

	/**
	 * 
	 * @param in
	 * @param days
	 * @return
	 * @pre in != null
	 */
	public static Date addDays(Date in, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(in);
		cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + days);
		return cal.getTime();
	}

	public static Date subDays(Date in, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(in);
		cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - days);
		return cal.getTime();
	}

	/**
	 * 
	 * @param in
	 * @param hours
	 * @return
	 * @pre in != null
	 */
	public static Date addHours(Date in, int hours) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(in);
		cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) + hours);
		return cal.getTime();
	}

	public static Date subHours(Date in, int hours) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(in);
		cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) - hours);
		return cal.getTime();
	}

	/**
	 * 
	 * @param in
	 * @param minutes
	 * @return
	 * @pre in != null
	 */
	public static Date addMinutes(Date in, int minutes) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(in);
		cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + minutes);
		return cal.getTime();
	}

	public static Date subMinutes(Date in, int minutes) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(in);
		cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) - minutes);
		return cal.getTime();
	}

	/**
	 * 
	 * @param in
	 * @param seconds
	 * @return
	 * @pre in != null
	 */
	public static Date addSeconds(Date in, int seconds) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(in);
		cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + seconds);
		return cal.getTime();
	}

	public static Date subSeconds(Date in, int seconds) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(in);
		cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) - seconds);
		return cal.getTime();
	}

	public static String getTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		Date currentTime = new Date();
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static Date getEndDate() {
		return addDays(new Date(), 1);
	}

	public static String getStrEndDate() {
		Date d = addDays(new Date(), 1);
		return formatDate(d);
	}

	public static Long getTodayMillsZero() {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime().getTime();
	}

	public static Long getTomorrowMillsZero() {
		Date date = addDays(new Date(), 1);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime().getTime();
	}

	
	private static SimpleDateFormat sf = null;

	public static String format(String time) {
		String formatDate = null;
		long parseLong = Long.parseLong(time);
		Date date = new Date(parseLong);
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		formatDate = sdf.format(date);
		return formatDate;
	}

	public static String format(Long time) {
		String formatDate = null;
		Date date = new Date(time);
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		formatDate = sdf.format(date);
		return formatDate;
	}

	/**
	 * 判断日期是星期几 如：2012-05-28
	 * 
	 * @param pTime
	 *            要判断的日期
	 */
	public static String getWeekDay(String today) {
		String week_day = today;
		try {
			String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
					"星期六" };
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
			Date d;
			d = f.parse(today);
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			int day = cal.get(Calendar.DAY_OF_WEEK);
			week_day = dayNames[day - 1];
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return week_day;
	}

	/**
	 * 获取当前的星期几
	 * 
	 * @param dt
	 * @return
	 */
	public static int getLocalWeekDate() {
		// {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 1;
		else if (w == 0) // 星期日
			w = 6;
		else
			w = w - 1;
		return w;
	}

	/* 获取系统时间 格式为："yyyy/MM/dd " */
	public static String getCurrentDate(String time) {
		Date d = new Date(time);
		sf = new SimpleDateFormat("yyyy-MM-dd");
		return sf.format(d);
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @param fromPattern
	 * @param toPatter
	 * @return
	 */
	public static String formatDate(String date, String fromPattern,
			String toPatter) {
		try {
			return new SimpleDateFormat(toPatter).format(new SimpleDateFormat(
					fromPattern).parse(date));
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return date;
	}

	/**
	 * 格式化日期 如：7月8日
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDateToMD(String date) {
		if (date.contains(":"))
			return formatDate(date, "yyyy-MM-dd HH:mm:ss", "M月d日");
		else
			return formatDate(date, "yyyy-MM-dd", "M月d日");
	}

	// String转换成DateTime

	public static Timestamp stringToDate(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = null; // 初始化date
		try {
			date = sdf.parse(str); // Mon Jan 14 00:00:00 CST 2013
		} catch (Exception e) {
			e.printStackTrace();
		}
		Timestamp timestamp = new Timestamp(date.getTime()); // 2013-01-14
																// 22:45:36.484
		return timestamp;
	}

	public static Date getCurrentUtilDate() {
		return new Date();
	}

	public static void main(String[] args) {
		// String str="2013-04-17 15:47:49.0";
		String str = "2013-04-17 15:47:49";
		Date tt = getDateFromOneDay(str, 1);
		System.out.println(tt);
		String mont = getCurrentPreMonth();
	}
}
